package com.xiyu.service.repository.material.info;

import com.xiyu.service.model.material.info.MaterialCategoryFetcher;
import com.xiyu.service.model.material.info.MaterialInfoFetcher;
import com.xiyu.service.vo.material.materialInfo.MaterialInfoPageQueryInput;
import com.xiyu.service.model.material.info.MaterialInfo;
import com.xiyu.service.model.material.info.MaterialInfoTable;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;
import java.util.Optional;

public interface MaterialInfoRepository extends JRepository<MaterialInfo, Long> {
    MaterialInfoTable materialInfoTable = MaterialInfoTable.$;

    default Page<MaterialInfo> pageQuery(MaterialInfoPageQueryInput inputVO){
        return sql().createQuery(materialInfoTable)
                .whereIf(StringUtils.hasText(inputVO.getMaterialName()), () -> materialInfoTable.materialName().like(inputVO.getMaterialName()))
                .whereIf(inputVO.getCategoryId() != null, materialInfoTable.categoryId().eq(inputVO.getCategoryId()))
                .orderBy(materialInfoTable.id().desc())
                .select(materialInfoTable.fetch(MaterialInfoFetcher.$.allTableFields().category(MaterialCategoryFetcher.$.allTableFields())))
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    Optional<MaterialInfo> findFirstByMaterialName(String materialName);


}